function Save(e) {
	var list = "";
	$(function() {
		$("#nourish_list").each(function() {
			$(this).children("option").each(function() {
				list += $(this).text() + ";";
			});
		});
	});
	
	if ( !checkID($("#id").val()) ) 
		return;
	else if ($("#height").val() == '') {
		alert("請填寫身高");
		return;
	} 
	else if ($("#weight").val() == '') {
		alert("請填寫體重");
		return;
	} 
	else if ($("#age").val() == '') {
		alert("請填寫年齡");
		return;
	}
	else if ($('input[name=activity]:checked').val() == null) {
		if ($('input[name=work]:checked').val() == null) {
			alert("請選擇工作量");
			return;
		}
	}
	
	$.ajax({
		type : "POST",
		url : "proSuggest.do",
		dataType : 'text',		
		
		data : {	
			type: "T",
			save: $("#save").val(),
			time : $("#time").val(),
			id : $("#id").val(),
			sex : $('input[name=sex]:checked').val(),
			age : $("#age").val(),
			height : $("#height").val(),
			weightType : $('input[name=weightType]:checked').val(),
			weight : $("#weight").val(),
			
			milk1 : $("#milk1").val(),			
			milk2 : $("#milk2").val(),
			milk3 : $("#milk3").val(),
			milk4 : $("#milk4").val(),
			milk5 : $("#milk5").val(),
			milk6 : $("#milk6").val(),
			low_milk1 : $("#low_milk1").val(),
			low_milk2 : $("#low_milk2").val(),
			low_milk3 : $("#low_milk3").val(),
			low_milk4 : $("#low_milk4").val(),
			low_milk5 : $("#low_milk5").val(),
			low_milk6 : $("#low_milk6").val(),
			high_milk1 : $("#high_milk1").val(),
			high_milk2 : $("#high_milk2").val(),
			high_milk3 : $("#high_milk3").val(),
			high_milk4 : $("#high_milk4").val(),
			high_milk5 : $("#high_milk5").val(),
			high_milk6 : $("#high_milk6").val(), 
			veg1 : $("#veg1").val(), 
			veg2 : $("#veg2").val(), 
			veg3 : $("#veg3").val(), 
			veg4 : $("#veg4").val(), 
			veg5 : $("#veg5").val(), 
			veg6 : $("#veg6").val(), 
			fruit1 : $("#fruit1").val(), 
			fruit2 : $("#fruit2").val(), 
			fruit3 : $("#fruit3").val(), 
			fruit4 : $("#fruit4").val(), 
			fruit5 : $("#fruit5").val(), 
			fruit6 : $("#fruit6").val(), 
			grain1 : $("#grain1").val(), 
			grain2 : $("#grain2").val(), 
			grain3 : $("#grain3").val(), 
			grain4 : $("#grain4").val(), 
			grain5 : $("#grain5").val(), 
			grain6 : $("#grain6").val(), 
			low_meat1 : $("#low_meat1").val(), 
			low_meat2 : $("#low_meat2").val(), 
			low_meat3 : $("#low_meat3").val(), 
			low_meat4 : $("#low_meat4").val(), 
			low_meat5 : $("#low_meat5").val(), 
			low_meat6 : $("#low_meat6").val(), 
			meat1 : $("#meat1").val(), 
			meat2 : $("#meat2").val(), 
			meat3 : $("#meat3").val(), 
			meat4 : $("#meat4").val(), 
			meat5 : $("#meat5").val(), 
			meat6 : $("#meat6").val(), 
			high_meat1 : $("#high_meat1").val(), 
			high_meat2 : $("#high_meat2").val(), 
			high_meat3 : $("#high_meat3").val(), 
			high_meat4 : $("#high_meat4").val(), 
			high_meat5 : $("#high_meat5").val(), 
			high_meat6 : $("#high_meat6").val(), 
			fat1 : $("#fat1").val(), 
			fat2 : $("#fat2").val(), 
			fat3 : $("#fat3").val(), 
			fat4 : $("#fat4").val(), 
			fat5 : $("#fat5").val(), 
			fat6 : $("#fat6").val(), 

			work : $('input[name=work]:checked').val(), 
			activity : $('input[name=activity]:checked').val(), 
			pressure : $("#pressure").val(), 
			g_kgBW : $("#g_kgBW").val(), 
			proportion_fat : $("#proportion_fat").val(), 

			quantity_Milk : $("#quantity_Milk").val(), 
			quantity_Veg : $("#quantity_Veg").val(), 
			quantity_Fruit : $("#quantity_Fruit").val(),
			
			type1 : $("#type1").val(),			
			type2 : $("#type2").val(),
			type3 : $("#type3").val(),
			type4 : $("#type4").val(),
			type5 : $("#type5").val(),
			type6 : $("#type6").val(),
			type7 : $("#type7").val(),			
			type8 : $("#type8").val(),
			type9 : $("#type9").val(),
			type10 : $("#type10").val(),
			type11 : $("#type11").val(),			
			type12 : $("#type12").val(),
			type13 : $("#type13").val(),
			type14 : $("#type14").val(),
			type15 : $("#type15").val(),			
			type16 : $("#type16").val(),
			type17 : $("#type17").val(),
			
			nourish_list : list,
			proportion_breakfast : $("#proportion_breakfast").val(),			
			proportion_lunch : $("#proportion_lunch").val(),
			proportion_dinner : $("#proportion_dinner").val(), 
			
			breakfast_dessert_Milk : $("#breakfast_dessert_Milk").val(),
			lunch_dessert_Milk : $("#lunch_dessert_Milk").val(), 
			dinner_dessert_Milk : $("#dinner_dessert_Milk").val(), 
			breakfast_dessert_Veg : $("#breakfast_dessert_Veg").val(), 
			lunch_dessert_Veg : $("#lunch_dessert_Veg").val(), 
			dinner_dessert_Veg : $("#dinner_dessert_Veg").val(), 
			breakfast_dessert_Fruit : $("#breakfast_dessert_Fruit").val(), 
			lunch_dessert_Fruit : $("#lunch_dessert_Fruit").val(), 
			dinner_dessert_Fruit : $("#dinner_dessert_Fruit").val(), 
			breakfast_dessert_Grain : $("#breakfast_dessert_Grain").val(), 
			lunch_dessert_Grain : $("#lunch_dessert_Grain").val(), 
			dinner_dessert_Grain : $("#dinner_dessert_Grain").val(), 
			breakfast_dessert_Meat : $("#breakfast_dessert_Meat").val(), 
			lunch_dessert_Meat : $("#lunch_dessert_Meat").val(), 
			dinner_dessert_Meat : $("#dinner_dessert_Meat").val(), 
			breakfast_dessert_Fat : $("#breakfast_dessert_Fat").val(), 
			lunch_dessert_Fat : $("#lunch_dessert_Fat").val(), 
			dinner_dessert_Fat : $("#dinner_dessert_Fat").val()
		},

		success : function(DataList) {
			alert(DataList);
		}
	});
}

function TempSave(e) {
	var list = "";
	$(function() {
		$("#nourish_list").each(function() {
			$(this).children("option").each(function() {
				list += $(this).text() + ";";
			});
		});
	});

	if ( !checkID($("#id").val()) ) 
		return;
	$.ajax({
		type : "POST",
		url : "proSuggest.do",
		dataType : 'text',		
		
		data : {	
			type: "F",
			save: $("#save").val(),	
			time : $("#time").val(),
			id : $("#id").val(),
			sex : $('input[name=sex]:checked').val(),
			age : $("#age").val(),
			height : $("#height").val(),
			weightType : $('input[name=weightType]:checked').val(),
			weight : $("#weight").val(),
			
			milk1 : $("#milk1").val(),			
			milk2 : $("#milk2").val(),
			milk3 : $("#milk3").val(),
			milk4 : $("#milk4").val(),
			milk5 : $("#milk5").val(),
			milk6 : $("#milk6").val(),
			low_milk1 : $("#low_milk1").val(),
			low_milk2 : $("#low_milk2").val(),
			low_milk3 : $("#low_milk3").val(),
			low_milk4 : $("#low_milk4").val(),
			low_milk5 : $("#low_milk5").val(),
			low_milk6 : $("#low_milk6").val(),
			high_milk1 : $("#high_milk1").val(),
			high_milk2 : $("#high_milk2").val(),
			high_milk3 : $("#high_milk3").val(),
			high_milk4 : $("#high_milk4").val(),
			high_milk5 : $("#high_milk5").val(),
			high_milk6 : $("#high_milk6").val(), 
			veg1 : $("#veg1").val(), 
			veg2 : $("#veg2").val(), 
			veg3 : $("#veg3").val(), 
			veg4 : $("#veg4").val(), 
			veg5 : $("#veg5").val(), 
			veg6 : $("#veg6").val(), 
			fruit1 : $("#fruit1").val(), 
			fruit2 : $("#fruit2").val(), 
			fruit3 : $("#fruit3").val(), 
			fruit4 : $("#fruit4").val(), 
			fruit5 : $("#fruit5").val(), 
			fruit6 : $("#fruit6").val(), 
			grain1 : $("#grain1").val(), 
			grain2 : $("#grain2").val(), 
			grain3 : $("#grain3").val(), 
			grain4 : $("#grain4").val(), 
			grain5 : $("#grain5").val(), 
			grain6 : $("#grain6").val(), 
			low_meat1 : $("#low_meat1").val(), 
			low_meat2 : $("#low_meat2").val(), 
			low_meat3 : $("#low_meat3").val(), 
			low_meat4 : $("#low_meat4").val(), 
			low_meat5 : $("#low_meat5").val(), 
			low_meat6 : $("#low_meat6").val(), 
			meat1 : $("#meat1").val(), 
			meat2 : $("#meat2").val(), 
			meat3 : $("#meat3").val(), 
			meat4 : $("#meat4").val(), 
			meat5 : $("#meat5").val(), 
			meat6 : $("#meat6").val(), 
			high_meat1 : $("#high_meat1").val(), 
			high_meat2 : $("#high_meat2").val(), 
			high_meat3 : $("#high_meat3").val(), 
			high_meat4 : $("#high_meat4").val(), 
			high_meat5 : $("#high_meat5").val(), 
			high_meat6 : $("#high_meat6").val(), 
			fat1 : $("#fat1").val(), 
			fat2 : $("#fat2").val(), 
			fat3 : $("#fat3").val(), 
			fat4 : $("#fat4").val(), 
			fat5 : $("#fat5").val(), 
			fat6 : $("#fat6").val(), 

			work : $('input[name=work]:checked').val(), 
			activity : $('input[name=activity]:checked').val(), 
			pressure : $("#pressure").val(), 
			g_kgBW : $("#g_kgBW").val(), 
			proportion_fat : $("#proportion_fat").val(), 

			quantity_Milk : $("#quantity_Milk").val(), 
			quantity_Veg : $("#quantity_Veg").val(), 
			quantity_Fruit : $("#quantity_Fruit").val(),
			
			type1 : $("#type1").val(),			
			type2 : $("#type2").val(),
			type3 : $("#type3").val(),
			type4 : $("#type4").val(),
			type5 : $("#type5").val(),
			type6 : $("#type6").val(),
			type7 : $("#type7").val(),			
			type8 : $("#type8").val(),
			type9 : $("#type9").val(),
			type10 : $("#type10").val(),
			type11 : $("#type11").val(),			
			type12 : $("#type12").val(),
			type13 : $("#type13").val(),
			type14 : $("#type14").val(),
			type15 : $("#type15").val(),			
			type16 : $("#type16").val(),
			type17 : $("#type17").val(),
			
			nourish_list : list,
			proportion_breakfast : $("#proportion_breakfast").val(),			
			proportion_lunch : $("#proportion_lunch").val(),
			proportion_dinner : $("#proportion_dinner").val(), 
			
			breakfast_dessert_Milk : $("#breakfast_dessert_Milk").val(),
			lunch_dessert_Milk : $("#lunch_dessert_Milk").val(), 
			dinner_dessert_Milk : $("#dinner_dessert_Milk").val(), 
			breakfast_dessert_Veg : $("#breakfast_dessert_Veg").val(), 
			lunch_dessert_Veg : $("#lunch_dessert_Veg").val(), 
			dinner_dessert_Veg : $("#dinner_dessert_Veg").val(), 
			breakfast_dessert_Fruit : $("#breakfast_dessert_Fruit").val(), 
			lunch_dessert_Fruit : $("#lunch_dessert_Fruit").val(), 
			dinner_dessert_Fruit : $("#dinner_dessert_Fruit").val(), 
			breakfast_dessert_Grain : $("#breakfast_dessert_Grain").val(), 
			lunch_dessert_Grain : $("#lunch_dessert_Grain").val(), 
			dinner_dessert_Grain : $("#dinner_dessert_Grain").val(), 
			breakfast_dessert_Meat : $("#breakfast_dessert_Meat").val(), 
			lunch_dessert_Meat : $("#lunch_dessert_Meat").val(), 
			dinner_dessert_Meat : $("#dinner_dessert_Meat").val(), 
			breakfast_dessert_Fat : $("#breakfast_dessert_Fat").val(), 
			lunch_dessert_Fat : $("#lunch_dessert_Fat").val(), 
			dinner_dessert_Fat : $("#dinner_dessert_Fat").val()
		},

		success : function(DataList) {
			alert(DataList);
		}
	});
}

function getid(e) {		//飲食建議歷史資料讀取
	var _id = $("#id").val();
	//清除頁面資料
	$("#nourish_list option").remove();		//營養品攝取清除
	$(":text").val("");		//所有text清除
	$("#id").val(_id); 
	$("#prot_").html("0");
	$("#prot_intake").html("0");
	$("#fat_").html("0");
	$("#fat_intake").html("0");
	$("#cho_").html("0");
	$("#cho_intake").html("0");
	$("#calofday_intake").html("0");
	
	$.ajax({
		type : "POST",
		url : "readHistory.do",
		dataType : 'text',		
		
		data : {
			type: "proSuggest",			
			id : $("#id").val()
		},
		
		success : function(data) {
			if (checkID($("#id").val())) {
				var str = data.split("|");
				
				if (str[0] == "nodata") {		//資料庫無此資料
					alert(str[1]);		
					location.reload();
				} 
				else {
					// sex radio
					if (str[0] == "male")
						$('input[name=sex]').get(0).checked = true;
					else if (str[0] == "female")
						$('input[name=sex]').get(1).checked = true;
					else
						$('input[name=sex]').attr("checked", false);
					// weightType
					if (str[1] == "old")
						$('input[name=weightType]').get(0).checked = true;
					else if (str[1] == "new")
						$('input[name=weightType]').get(1).checked = true;
					else
						$('input[name=weightType]').attr("checked", false);
					// work radio
					if (str[2] == "little")
						$('input[name=work]').get(0).checked = true;
					else if (str[2] == "middle")
						$('input[name=work]').get(1).checked = true;
					else if (str[2] == "large")
						$('input[name=work]').get(2).checked = true;
					else
						$('input[name=work]').attr("checked", false);
					// activity radio
					if (str[3] == "little")
						$('input[name=activity]').get(0).checked = true;
					else if (str[3] == "middle")
						$('input[name=activity]').get(1).checked = true;
					else if (str[3] == "large")
						$('input[name=activity]').get(2).checked = true;
					else
						$('input[name=activity]').attr("checked", false);
					// select data
					if (str[4]) {
						var getdata = str[4].split(";");
						for ( var i = 0; i < getdata.length; i++) {
							if (getdata[i])
								nourish_list(getdata[i]); // 營養品攝取份量計算
						}
					}					
					// text data
					for ( var i = 5; i < str.length; i++) {
						var getdata = str[i].split(":");
						$(getdata[0]).val(getdata[1]);
					}
					
					get_sqldata($('input[name=sex]:checked').val(), $(
							'input[name=weightType]:checked').val(), $(
							'input[name=work]:checked').val(), $(
							'input[name=activity]:checked').val());
									
					Total_intake();
				}
			} 
			else   // if ( !checkID(id) )
				location.reload();
		}
	});
}

function SymptomsClass(e) {  //營養品產品名
	$.ajax({
		type : "POST",
		url : "maintenanceNourishment.do",
		dataType : 'text',
		
		data : {
			type : "getproductname",
			company : $(this).val()
		},

		error : function(xhr) {
			alert("Ajax request error!");
		},
		
		success : function(DataList) {
			$("#SymptomsClasstest").html(DataList);	
		}
	});
}

function add_list(e) {
	if ($("#SymptomsClass").val() == '請選擇') {
		alert("請選擇營養品公司");
		return;
	} 
	else if ($("#SymptomsClasstest").val() == '請選擇') {
		alert("請選擇營養品產品");
		return;
	} 
	
	$.ajax({
		type : "POST",
		url : "maintenanceNourishment.do",
		dataType : 'text',	
		
		data: {
			type: "addlist",
			nourish_list: $("#nourish_list").html(),
			company: $("#SymptomsClass").val(),
			productname: $("#SymptomsClasstest").val(),
			nourish_quantity: $("#nourish_quantity").val(),
			nourish_meal: $("#nourish_meal").val()
		},
		
		success : function(DataList) {
			if (DataList.match("營養品填寫錯誤")) 
				alert(DataList);
			else 
				nourish_list(DataList); //營養品攝取份量計算
		}
	});
}

function nourish_list(e) {	//營養品攝取份量計算
	var str, add_prot=0, add_fat=0, add_cho=0, cal=0;

	$.ajax({
		success: function () {
			str = e.split(",");

			for ( var i = 2; i < str.length; i++) { 
				var a = str[i].split("：");
				if (a[0].match("蛋白質"))   //公克
					add_prot = parseFloat(a[1]);
				else if (a[0].match("脂肪"))   //公克
					add_fat = parseFloat(a[1]);
				else if (a[0].match("醣類"))  //公克
					add_cho = parseFloat(a[1]);	
			}
			
			var prot_g = parseFloat($("#prot_intake").html()) + parseFloat(add_prot);
			var fat_g = parseFloat($("#fat_intake").html()) + parseFloat(add_fat);
			var cho_g = parseFloat($("#cho_intake").html()) + parseFloat(add_cho);
			
			var prot_ = parseFloat(prot_g * 4);
			var fat_ = parseFloat(fat_g * 9);
			var cho_ = parseFloat(cho_g * 4);
		
			cal = prot_ + fat_ + cho_;

			var prot_rate = prot_ / cal * 100;
			var fat_rate = fat_ / cal * 100;
			var cho_rate = cho_ / cal * 100;
			var weight = parseFloat($("#weight").val());

			$("#nourish_list").append($("<option></option>").attr("value", e).text(e));			
			$("#prot_intake").html(prot_g);			
			$("#prot_").html(prot_);
			$("#prot_rate").html( Math.round(prot_rate) );
			
			$("#fat_intake").html(fat_g);			
			$("#fat_").html(fat_);
			$("#fat_rate").html( Math.round(fat_rate) );
			
			$("#cho_intake").html(cho_g);
			$("#cho_").html(cho_);
			$("#cho_rate").html( Math.round(cho_rate) );
			
			$("#calofday_intake").html(parseFloat(cal));
			$("#average1").html("(" + Math.round(cal/weight) + "大卡/公斤)");
			

			Save_nourish_val("add", parseFloat(add_prot), parseFloat(add_fat), parseFloat(add_cho));
			drawChart(prot_, fat_, cho_, "chart1");  //繪製圓餅圖
			Compare();		//飲食建議與攝取比較
		}
	});
}

function del_list(e) {
	var del_prot=0, del_fat=0, del_cho=0, cal=0;
	$.ajax({
		success: function (){		
			
			var sel_item = $("#nourish_list").find("option:selected").text();
			if (sel_item != "") {
				var str = sel_item.split(",");
				for ( var i = 2; i < str.length; i++) {
					var a = str[i].split("：");
					if (a[0].match("蛋白質"))
						del_prot = parseFloat(a[1]);
					else if (a[0].match("脂肪"))
						del_fat = parseFloat(a[1]);
					else if (a[0].match("醣類"))
						del_cho = parseFloat(a[1]);
				}

				cal = parseFloat($("#calofday_intake").html())
						- (del_prot * 4 + del_fat * 9 + del_cho * 4);
				var prot_g = parseFloat($("#prot_intake").html()) - del_prot;
				var fat_g = parseFloat($("#fat_intake").html()) - del_fat;
				var cho_g = parseFloat($("#cho_intake").html()) - del_cho;
				
				var prot_ = parseFloat(prot_g * 4);
				var fat_ = parseFloat(fat_g * 9);
				var cho_ = parseFloat(cho_g * 4);
				
				var prot_rate = prot_ / cal * 100;
				var fat_rate = fat_ / cal * 100;
				var cho_rate = cho_ / cal * 100;
				var weight = parseFloat($("#weight").val());

				// alert(cal);
				$("#nourish_list").find("option:selected").remove();
				$("#prot_intake").html(prot_g);			
				$("#prot_").html(prot_);
				$("#prot_rate").html( Math.round(prot_rate) );
				
				$("#fat_intake").html(fat_g);			
				$("#fat_").html(fat_);
				$("#fat_rate").html( Math.round(fat_rate) );
				
				$("#cho_intake").html(cho_g);
				$("#cho_").html(cho_);
				$("#cho_rate").html( Math.round(cho_rate) );
				
				$("#calofday_intake").html(parseFloat(cal));
				$("#average1").html("(" + Math.round(cal/weight) + "大卡/公斤)");

				drawChart(prot_, fat_, cho_, "chart1");  //繪製圓餅圖
				Compare(); // 飲食建議與攝取比較
				Save_nourish_val("del", parseFloat(del_prot), parseFloat(del_fat), parseFloat(del_cho));
			} 
		}
	});
}

$(document).ready(function(e) {
	$("#Save").on("click", Save);
	$("#TempSave").on("click", TempSave);
	//$("#getdata").on("click", getid);
	$("#SymptomsClass").on("change", SymptomsClass);
	$("#SymptomsClasstest").html("<option>請選擇</option>");
	$("#add_list").on("click", add_list);
	$("#del_list").on("click", del_list);

});
